import { useMemo } from 'react';

import type { MessagesContextValue } from '../../../contexts/messagesContext/MessagesContext';

export const useCreateMessagesContext = ({
  additionalPressableProps,
  Attachment,
  AttachmentActions,
  AudioAttachment,
  Card,
  CardCover,
  CardFooter,
  CardHeader,
  channelId,
  customMessageSwipeAction,
  DateHeader,
  deletedMessagesVisibilityType,
  deleteMessage,
  deleteReaction,
  disableTypingIndicator,
  dismissKeyboardOnMessageTouch,
  enableMessageGroupingByUser,
  enableSwipeToReply,
  FileAttachment,
  FileAttachmentGroup,
  FileAttachmentIcon,
  FlatList,
  forceAlignMessages,
  Gallery,
  getMessagesGroupStyles,
  Giphy,
  giphyVersion,
  handleBan,
  handleCopy,
  handleDelete,
  handleDeleteForMe,
  handleEdit,
  handleFlag,
  handleMarkUnread,
  handleMute,
  handlePinMessage,
  handleQuotedReply,
  handleReaction,
  handleRetry,
  handleThreadReply,
  hasCreatePoll,
  ImageLoadingFailedIndicator,
  ImageLoadingIndicator,
  ImageReloadIndicator,
  initialScrollToFirstUnreadMessage,
  InlineDateSeparator,
  InlineUnreadIndicator,
  isAttachmentEqual,
  isMessageAIGenerated,
  legacyImageViewerSwipeBehaviour,
  markdownRules,
  Message,
  MessageActionList,
  MessageActionListItem,
  messageActions,
  MessageAvatar,
  MessageBounce,
  MessageContent,
  messageContentOrder,
  MessageDeleted,
  MessageEditedTimestamp,
  MessageError,
  MessageFooter,
  MessageHeader,
  MessageList,
  MessageLocation,
  MessageMenu,
  MessagePinnedHeader,
  MessageReactionPicker,
  MessageReplies,
  MessageRepliesAvatars,
  MessageSimple,
  MessageStatus,
  MessageSwipeContent,
  messageSwipeToReplyHitSlop,
  MessageSystem,
  MessageText,
  messageTextNumberOfLines,
  MessageTimestamp,
  MessageUserReactions,
  MessageUserReactionsAvatar,
  MessageUserReactionsItem,
  myMessageTheme,
  onLongPressMessage,
  onPressInMessage,
  onPressMessage,
  PollContent,
  ReactionListBottom,
  reactionListPosition,
  ReactionListTop,
  removeMessage,
  Reply,
  retrySendMessage,
  ScrollToBottomButton,
  selectReaction,
  sendReaction,
  shouldShowUnreadUnderlay,
  StreamingMessageView,
  supportedReactions,
  targetedMessage,
  TypingIndicator,
  TypingIndicatorContainer,
  UnreadMessagesNotification,
  updateMessage,
  UrlPreview,
  VideoThumbnail,
}: MessagesContextValue & {
  /**
   * To ensure we allow re-render, when channel is changed
   */
  channelId?: string;
}) => {
  const additionalTouchablePropsLength = Object.keys(additionalPressableProps || {}).length;
  const markdownRulesLength = Object.keys(markdownRules || {}).length;
  const messageContentOrderValue = messageContentOrder.join();
  const supportedReactionsLength = supportedReactions?.length;

  const messagesContext: MessagesContextValue = useMemo(
    () => ({
      additionalPressableProps,
      Attachment,
      AttachmentActions,
      AudioAttachment,
      Card,
      CardCover,
      CardFooter,
      CardHeader,
      customMessageSwipeAction,
      DateHeader,
      deletedMessagesVisibilityType,
      deleteMessage,
      deleteReaction,
      disableTypingIndicator,
      dismissKeyboardOnMessageTouch,
      enableMessageGroupingByUser,
      enableSwipeToReply,
      FileAttachment,
      FileAttachmentGroup,
      FileAttachmentIcon,
      FlatList,
      forceAlignMessages,
      Gallery,
      getMessagesGroupStyles,
      Giphy,
      giphyVersion,
      handleBan,
      handleCopy,
      handleDelete,
      handleDeleteForMe,
      handleEdit,
      handleFlag,
      handleMarkUnread,
      handleMute,
      handlePinMessage,
      handleQuotedReply,
      handleReaction,
      handleRetry,
      handleThreadReply,
      hasCreatePoll,
      ImageLoadingFailedIndicator,
      ImageLoadingIndicator,
      ImageReloadIndicator,
      initialScrollToFirstUnreadMessage,
      InlineDateSeparator,
      InlineUnreadIndicator,
      isAttachmentEqual,
      isMessageAIGenerated,
      legacyImageViewerSwipeBehaviour,
      markdownRules,
      Message,
      MessageActionList,
      MessageActionListItem,
      messageActions,
      MessageAvatar,
      MessageBounce,
      MessageContent,
      messageContentOrder,
      MessageDeleted,
      MessageEditedTimestamp,
      MessageError,
      MessageFooter,
      MessageHeader,
      MessageList,
      MessageLocation,
      MessageMenu,
      MessagePinnedHeader,
      MessageReactionPicker,
      MessageReplies,
      MessageRepliesAvatars,
      MessageSimple,
      MessageStatus,
      MessageSwipeContent,
      messageSwipeToReplyHitSlop,
      MessageSystem,
      MessageText,
      messageTextNumberOfLines,
      MessageTimestamp,
      MessageUserReactions,
      MessageUserReactionsAvatar,
      MessageUserReactionsItem,
      myMessageTheme,
      onLongPressMessage,
      onPressInMessage,
      onPressMessage,
      PollContent,
      ReactionListBottom,
      reactionListPosition,
      ReactionListTop,
      removeMessage,
      Reply,
      retrySendMessage,
      ScrollToBottomButton,
      selectReaction,
      sendReaction,
      shouldShowUnreadUnderlay,
      StreamingMessageView,
      supportedReactions,
      targetedMessage,
      TypingIndicator,
      TypingIndicatorContainer,
      UnreadMessagesNotification,
      updateMessage,
      UrlPreview,
      VideoThumbnail,
    }),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [
      additionalTouchablePropsLength,
      channelId,
      disableTypingIndicator,
      dismissKeyboardOnMessageTouch,
      initialScrollToFirstUnreadMessage,
      markdownRulesLength,
      messageContentOrderValue,
      supportedReactionsLength,
      myMessageTheme,
      targetedMessage,
      hasCreatePoll,
    ],
  );

  return messagesContext;
};
